package dk.modified.reshary;

import java.util.ArrayList;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import dk.modified.reshary.GlobalState.TrackerName;

public class SplashScreen extends Activity {

	private Button cmd_network_retry;
	private RelativeLayout network_retry;
	private ImageView splashscreenimage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen);
		Tracker t = ((GlobalState) getApplication())
				.getTracker(TrackerName.APP_TRACKER);
		t.setScreenName("Reshary Splashscreen");
		t.send(new HitBuilders.AppViewBuilder().build());

		cmd_network_retry = (Button) findViewById(R.id.cmd_network_retry);
		network_retry = (RelativeLayout) findViewById(R.id.network_retry);
		splashscreenimage = (ImageView) findViewById(R.id.splashscreenimage);
		setOnClickListeners();

		SharedPreferences sharedpreferences = getSharedPreferences(
				"dk.modified.reshary", Context.MODE_PRIVATE);
		String userid = sharedpreferences.getString("userid", "null");
		String securitytoken = sharedpreferences.getString("securitytoken",
				"null");

		if (!userid.equals("null") && (!securitytoken.equals("null"))) {
			GetCategories();

			return;
		}
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			if (extras.getString("failure") != null) {
				Toast.makeText(getApplicationContext(),
						extras.getString("failure"), Toast.LENGTH_LONG).show();
			}
		}

		if (userid.equals("null") && securitytoken.equals("null")) {
			Intent loggedin = new Intent(getApplicationContext(),
					LoginOrSignup.class);
			loggedin.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(loggedin);
		}

	}

	private void setOnClickListeners() {

		cmd_network_retry.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				network_retry.setVisibility(View.GONE);
				splashscreenimage.setVisibility(View.VISIBLE);
				GetCategories();
			}
		});

	}

	private void GetCategories() {

		SharedPreferences sharedpreferences = getSharedPreferences(
				"dk.modified.reshary", Context.MODE_PRIVATE);
		String userid = sharedpreferences.getString("userid", "null");
		String securitytoken = sharedpreferences.getString("securitytoken",
				"null");

		RequestParams params = new RequestParams();
		params.put("userid", userid);
		params.put("securitytoken", securitytoken);

		ResharyRestClient.post("requestcategories/", params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						// TODO Auto-generated method stub
						network_retry.setVisibility(View.VISIBLE);
						splashscreenimage.setVisibility(View.GONE);
					}

					@Override
					public void onSuccess(String content) {
						// TODO Auto-generated method stub

						if (content.contains("Invalid securitytoken")) {
							Util.intentSendBackUser(SplashScreen.this);
						}

						Map<String, Object> postResponse = Util
								.specialformatResponseCategories(content);
						
						if(postResponse.containsKey("report_posts")){
//							String report_posts = (String) postResponse.get("report_posts");
							SharedPreferences sharedpreferences = getApplicationContext()
									.getSharedPreferences(
											"dk.modified.reshary",
											Context.MODE_PRIVATE);
							Editor editor = sharedpreferences.edit();
							editor.putString("report_posts", content);
							editor.commit();
						}
						
						if (postResponse.containsKey("error")) {
							Util.intentSendBackUser(SplashScreen.this);

						} else {
							@SuppressWarnings("unchecked")
							ArrayList<Category> arrOfCategories = (ArrayList<Category>) postResponse
									.get("cat");
//							ArrayPlaceholder.CategoryArr = arrOfCategories;

							Intent loggedin = new Intent(
									getApplicationContext(), MainActivity.class);
							loggedin.putParcelableArrayListExtra("categoryArr", arrOfCategories);
							loggedin.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
									| Intent.FLAG_ACTIVITY_CLEAR_TASK);
							startActivity(loggedin);
							overridePendingTransition(R.anim.fadeout,
									R.anim.fadein);

						}
					}

				}, this);
	}
}
